package com.gs.weather.service.other;

import com.gs.weather.dao.MicapsGeologyDao;
import com.gs.weather.utils.DateUtils;
import com.gs.weather.utils.HttpUtils;
import com.gs.weather.entity.MicapsResolve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/9 0009
 */
@Service
@EnableScheduling
public class MicapsGeologyService {

    @Autowired
    MicapsGeologyDao dao;

    private String lastTime = "";

    public String getLastTime() {
        return lastTime;
    }

    public void setLastTime(String lastTime) {
        this.lastTime = lastTime;
    }

    private final static MicapsGeologyService instance = new MicapsGeologyService();

    @Scheduled(cron = "0 0/3 *  * * ?")//每3分钟运行一次
    //@Scheduled(cron = "0/10 * *  * * ?")//test
    public void getMaxData() {
        List<MicapsResolve> maxDatas = new LinkedList();
        List<MicapsResolve> aimDatas = new LinkedList<>();

        String currentTime = DateUtils.getYearToMinuteStr();

        if (instance.getLastTime().equals(currentTime)) return;
        //System.out.println("lastTime: " + instance.getLastTime() + "   currentTime: " + currentTime);//test
        try {
            instance.setLastTime(currentTime);
            Map map = DateUtils.getYearMonthDay();
            String rootPath = "smb://tiga:tiga1234@10.135.30.179/realtimedata/disa/risk/Grid_dzzh/" + map.get("year") + "/" + map.get("month") + "/" + map.get("day") + "/";
            SmbFile smbFile = new SmbFile(rootPath);
            if (!smbFile.exists()) return;
            SmbFile[] smbFiles = smbFile.listFiles((SmbFile file) -> (file.getName().contains(currentTime)));
            //File testFile = new File("src/GEO201709261720_12h_001.dat");//test
            BufferedReader br = null;
            // 建立smb文件输入流
            double lngGrid = 0;//经度格距
            double latGrid = 0;//纬度格距
            double lngStart = 0;//起始经度
            double latStart = 0;//起始纬度
            double lngNum = 0;//经向格点数
            double latNum = 0;//纬向格点数
            String line = "";
            //for (int i = 0; i < 1; i++) {//test
            for (int i = 0; i < smbFiles.length; i++) {
                try {
                    int row = 1;
                    // br = new BufferedReader(new InputStreamReader(new FileInputStream(testFile)));//test
                    br = new BufferedReader(new InputStreamReader(new SmbFileInputStream(smbFiles[i])));
                    while ((line = br.readLine()) != null) {
                        if (row == 2) {
                            String[] strs = line.split(" ");
                            lngGrid = Double.parseDouble(strs[6]);
                            latGrid = Double.parseDouble(strs[7]);
                            lngStart = Double.parseDouble(strs[8]);
                            latStart = Double.parseDouble(strs[10]);
                        }
                        if (row == 3) {
                            String[] strs = line.split(" ");
                            latNum = Double.parseDouble(strs[0]);
                            lngNum = Double.parseDouble(strs[1]);
                        }
                        if (row == 4) {
                            String[] datas = line.split(" ");
                            for (int m = 0; m < lngNum; m++) {
                                for (int n = 0; n < latNum; n++) {
                                    if (!datas[(int) (m * latNum + n)].equals("9999") && !datas[(int) (m * latNum + n)].equals("0")) {
                                        MicapsResolve micapsResolve = new MicapsResolve(lngStart + lngGrid * m, latStart + latGrid * n, Double.parseDouble(datas[(int) (m * latNum + n)]), smbFiles[i].getName().substring(3, smbFiles[i].getName().length() - 4));
                                        //MicapsResolve micapsResolve = new MicapsResolve(lngStart + lngGrid * m, latStart + latGrid * n, Double.parseDouble(datas[(int) (m * latNum + n)]), testFile.getName().substring(3, testFile.getName().length() - 4));//test
                                        maxDatas.add(micapsResolve);
                                    }
                                }
                            }
                        }
                        row++;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            if (maxDatas.size() != 0) {
                for (int i = 0; i < maxDatas.size(); i++) {
                    String url = "http://10.135.30.12/services/GeoService.asmx/FindLocation?longitude=" + maxDatas.get(i).getLng() + "&latitude=" + maxDatas.get(i).getLat();
                    String areaName = HttpUtils.request(url);
                    if (areaName.contains("萧山区")) {
                        maxDatas.get(i).setAreaName(areaName.substring(areaName.indexOf("浙江省,") + 4, areaName.indexOf("</string>")));
                        aimDatas.add(maxDatas.get(i));
                    }
                }
                if (aimDatas.size()==0) System.out.println("当前时间萧山区没有灾害点信息");
                else dao.addGeology(aimDatas);
            } else {
                System.out.println("当前时间浙江省没有灾害点信息");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
